import cv2
import math

path = "Resources/angle.png"
img = cv2.imread(path)

pointlist = []

def gradient(pt1,pt2):
    return (pt2[1]-pt1[1])/(pt2[0]-pt1[0])

def mousePoints(event,x,y,flags,params):
    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.circle(img,(x,y),5,(0,0,255),cv2.FILLED)
        # print(pointlist)
        size = len(pointlist)
        if (size)%3!=0 and size %3!=0:
            cv2.line(img,tuple(pointlist[round((size-1)/3)*3]),(x,y),(0,255,255),2)
        pointlist.append([x,y])

def getAngle():
    pt1,pt2,pt3 = pointlist[-3:]
    m1 = gradient(pt1,pt2)
    m2 = gradient(pt1,pt3)
    angR = math.atan(abs((m1-m2)/(1+m1*m2)))
    angD = round(math.degrees(angR))
    return angD

while True:
    cv2.imshow("Image", img)
    cv2.setMouseCallback("Image",mousePoints)

    if len(pointlist)%3==0 and len(pointlist)!=0:
        angle = getAngle()
        temp = pointlist[-1:][0]
        cv2.putText(img,str(angle),(temp[0],temp[1]),cv2.FONT_HERSHEY_COMPLEX,1,(0,0,255),2)
        print(angle)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        pointlist=[]
        img = cv2.imread(path)












